//
//  ZZColorRGBA.m
//  ZZBlue
//
//  Created by stonesMonkey on 2017/9/22.
//  Copyright © 2017年 stonesMonkey. All rights reserved.
//

#import "ZZColorRGBA.h"
#import "UIColor+ZUtilities.h"

@implementation ZZColorRGBA

- (instancetype)init {
    
    if (self= [super init]) {
        
        self.A = 1.0;
    }
    return self;
}

+ (instancetype)colorRGBAWithR:(CGFloat)R G:(CGFloat)G B:(CGFloat)B A:(CGFloat)A {
    
    return [[self alloc] initWithR:R G:G B:B A:A];
}

- (instancetype)initWithR:(CGFloat)R G:(CGFloat)G B:(CGFloat)B A:(CGFloat)A {
    
    if (self = [super init]) {
        
        self.R = R;
        self.G = G;
        self.B = B;
        self.A = A;
    }
    
    return self;
}


-(void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeInteger:self.R forKey:@"R"];
    [aCoder encodeInteger:self.G forKey:@"G"];
    [aCoder encodeInteger:self.B forKey:@"B"];
    [aCoder encodeInteger:self.A forKey:@"A"];
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super init]) {
        
        self.R = [aDecoder decodeIntegerForKey:@"R"];
        self.G = [aDecoder decodeIntegerForKey:@"G"];
        self.B = [aDecoder decodeIntegerForKey:@"B"];
        self.A = [aDecoder decodeIntegerForKey:@"A"];
    }
    return self;
}

-(id)copy {
    
    ZZColorRGBA *newRGBA = [ZZColorRGBA new];
    newRGBA.R = self.R;
    newRGBA.B = self.B;
    newRGBA.G = self.G;
    newRGBA.A = self.A;
    return newRGBA;
}

- (UIColor *)color {
    
    return [UIColor zColorWithRed:self.R green:self.G blue:self.B];
}

+ (ZZColorRGBA *)rgbaFromColor:(UIColor *)color {
    
   NSArray *arr = [UIColor colorForGGB:color];
    ZZColorRGBA *rgba = [ZZColorRGBA new];
    rgba.R = [arr[0] floatValue];
    rgba.G = [arr[1] floatValue];
    rgba.B = [arr[2] floatValue];
    rgba.A = 1;
    
    return rgba;
}

+ (NSArray<UIColor *> *)colorsFromRGBA:(NSArray<ZZColorRGBA *> *)rgbas {
    
    NSMutableArray *arr = [NSMutableArray array];
    for (NSInteger index = 0; index< rgbas.count; index++) {
        
        [arr addObject:[rgbas[index] color]];
    }

    return arr;
}

+ (NSArray <ZZColorRGBA *>*)rgbasFromColors:(NSArray <UIColor *>*)colors {
    
    NSMutableArray *arr = [NSMutableArray array];
    for (NSInteger index = 0; index < arr.count; index++) {
        
        [arr addObject: [ZZColorRGBA rgbaFromColor:colors[index]]];
    }
    return arr;
}

@end
